import { ipcMain } from 'electron-better-ipc'
import * as fs from 'node:fs'
import { getWorkSpaceDesignDataFilePathById } from './paths'
import { readJson, writeJson } from './utils'

// 读取历史记录数据
export const readDesignData = (workSpaceId: string) => {
    const filepath = getWorkSpaceDesignDataFilePathById(workSpaceId)
    // 历史打开的工作空间列表
    if (!fs.existsSync(filepath)) {
        writeJson(filepath, [])
    }
    console.log('readDesignData')
    return readJson(filepath) as any[]
}

// 保存
export const saveDesignData = ({ workSpaceId, data }: { workSpaceId: string; data: any[] }) => {
    console.log('保存设计数据', data)
    writeJson(getWorkSpaceDesignDataFilePathById(workSpaceId), data)
}

ipcMain.answerRenderer('readDesignData', readDesignData)
ipcMain.answerRenderer('saveDesignData', saveDesignData)
